﻿Public Class FtpConnector

    Private isShowProgress As Boolean = True
    Private response As String

    Public Property showProgress() As Boolean
        Get
            Return isShowProgress
        End Get
        Set(ByVal value As Boolean)
            isShowProgress = value
        End Set
    End Property

    Public ReadOnly Property getResponse() As String
        Get
            Return response
        End Get
    End Property

    Public Sub upload(ByVal _localFile As String, ByVal _destinationFTPSite As String, ByVal _destinationPath As String, ByVal _destinationFileName As String)
        If My.Computer.FileSystem.FileExists(_localFile) And My.Computer.Network.IsAvailable Then
            Try
                My.Computer.Network.UploadFile(_localFile, _destinationFTPSite & _destinationPath & _destinationFileName, My.Settings.ftp_login, My.Settings.ftp_pass, isShowProgress, 5000, FileIO.UICancelOption.DoNothing)
                response = "OK"
            Catch ex As Exception
                response = ex.Message & vbNewLine
            End Try


        Else
            If My.Computer.Network.IsAvailable = False Then
                MessageBox.Show("No network detected?")
            End If
            If My.Computer.FileSystem.FileExists(_localFile) Then
                MessageBox.Show("The source file to be uplaoded does not seem to exist. " & _localFile)
            End If
        End If
    End Sub

    Public Sub download(ByVal _downloadToDirectory As String, ByVal _downloadToFileName As String, ByVal _sourceFTPSite As String, ByVal _sourceFTPDir As String, ByVal sourceFTPFileName As String)
        '"ftp://tecnikk.co.uk/httpdocs/var/export/")
        If My.Computer.Network.IsAvailable Then
            Try
                If Not My.Computer.FileSystem.DirectoryExists(_downloadToDirectory) Then
                    MessageBox.Show(_downloadToDirectory & " does not exist. Will create one now and download will continue.")
                    Try
                        My.Computer.FileSystem.CreateDirectory(_downloadToDirectory)
                    Catch ex As Exception
                        MessageBox.Show(ex.ToString & vbNewLine & "Cannot create directory! Now Exiting!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                        Exit Sub
                    End Try

                End If
                My.Computer.Network.DownloadFile(_sourceFTPSite & _sourceFTPDir & sourceFTPFileName, _downloadToDirectory & _downloadToFileName, My.Settings.ftp_login, My.Settings.ftp_pass, isShowProgress, 5000, True, FileIO.UICancelOption.DoNothing)
            Catch ex As Exception
                MessageBox.Show(ex.Message.ToString & vbNewLine & "Downlaod to:" & _downloadToDirectory & _downloadToFileName & vbNewLine & "Download From: " & _sourceFTPSite & _sourceFTPDir & sourceFTPFileName)
            End Try

        Else
            If My.Computer.Network.IsAvailable = False Then
                MessageBox.Show("No network detected?")
            End If
        End If
    End Sub
End Class
